#include <QtGui>

#include "Datumeditor.h"

Datumeditor::Datumeditor(QObject *eltern) : QStyledItemDelegate(eltern)
{
}
QString Datumeditor::displayText(const QVariant &wert, const QLocale &sprache) const
{
  QDateTime Datum=QDateTime::fromTime_t(wert.toUInt()).toLocalTime();
  return Datum.toString(sprache.dateFormat());
}
QWidget* Datumeditor::createEditor(QWidget *eltern, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
  if(!index.isValid())
    return QStyledItemDelegate::createEditor(eltern,option,index);
  QDateEdit* Editor=new QDateEdit(eltern);
  Editor->setCalendarPopup(true);
  Editor->calendarWidget()->setFirstDayOfWeek(QLocale::system().firstDayOfWeek());
  Editor->setDisplayFormat(QLocale().dateFormat(QLocale::LongFormat));
  return Editor;
}
void Datumeditor::setEditorData(QWidget *editor, const QModelIndex &index) const
{
  QDateEdit* Editor=qobject_cast<QDateEdit *>(editor);
  QDateTime Datum=QDateTime::fromTime_t(index.data().toUInt()).toLocalTime();
  Editor->setDate(Datum.date());
}
void Datumeditor::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
  QDateEdit* Editor=qobject_cast<QDateEdit *>(editor);
  QDateTime Datum;
  Datum.setTimeSpec(Qt::UTC);
  Datum.setDate(Editor->date());
  model->setData(index,Datum.toTime_t());
}
